//
//  XFBaseController.m
//  MYManager
//
//  Created by ukongm on 17/7/4.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "MYBaseController.h"
@interface MYBaseController ()


@end

@implementation MYBaseController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    if (@available(iOS 11.0, *)) {
        
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self.view addSubview:self.contentView];
    
    typeof(self)sself = self;
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(sself.mas_topLayoutGuide);
        make.bottom.equalTo(sself.mas_bottomLayoutGuide);
        make.left.right.equalTo(sself.view);
    }];
    
    [self settingSystemNavi];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)settingSystemNavi{
//    UILabel* label = (UILabel*)self.navigationItem.titleView;
//    
//    label.textColor = [UIColor whiteColor];
//    
//    self.navigationItem.titleView = label;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:NAVI_TITLE_FONT],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.navigationController.navigationBar.barTintColor = [UIColor naviColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
//    [button setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
//    [button setImage:[UIImage imageNamed:@"btn_back@3x"] forState:UIControlStateNormal];
    
//    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back@3x"] style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)dealloc{
    self.refresh = NO;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [self viewDidLayoutSubviews];
}

#pragma mark - 其他
- (void)setHome:(BOOL)home{
    _home = home;
    
    if (home) {
        [self.tableView changeHeight:self.view.height - 49 - 64];
    }else{
        [self.tableView changeHeight:self.view.height - 64];
    }
}

#pragma mark - 活动提示框
- (XFActiveManager *)activeManager{
    
    if (_activeManager == nil) {
        _activeManager = [XFActiveManager new];
    }
    return _activeManager;
}

#pragma mark - 请求相关
- (void)succeed:(NSData *)data andMark:(NSString *)mark{
    
    NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    //有一个指标
    if (dict != nil && ![dict[@"status"] isEqualToString:@"error"]) {
        [self succeedForSuccess:dict mark:mark];
    }else{
        [self succeedForFail:dict mark:mark];
    }
}

- (void)succeedForSuccess:(NSDictionary *)dict mark:(NSString *)mark{
    
    XFLog(@"%@ 有数据返回，但是没有处理数据",self.class);
}

- (void)succeedForFail:(NSDictionary*)dict mark:(NSString*)mark{
    
    if (self.activeManager.status == XFActiveManagerStatusActive) {
        [self.activeManager failForValue:dict[@"data"][@"ErrMsg"]];
    }
}

- (void)fail:(NSError *)error andMark:(NSString *)mark{
    if (self.activeManager.status == XFActiveManagerStatusActive) {
        [self.activeManager fail];
    }
}

- (void)handle{
    if (self.header) {
         [self.header endRefreshing];
    }
    
    if (self.footer) {
        
        [self.footer endRefreshing];
    }
    
    self.downloading = NO;
}

#pragma mark - 列表相关

- (UITableView *)tableView{
    
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 64) style:UITableViewStyleGrouped];
        
        [_tableView setContentInset:UIEdgeInsetsZero];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        _tableView.backgroundColor = [UIColor red:238 green:238 blue:238];
    }
    return _tableView;
}

- (void)setRefresh:(BOOL)refresh{
    self.headRefresh = refresh;
    self.footRefresh = refresh;
}

- (void)setHeadRefresh:(BOOL)headRefresh{
    typeof(self)sself = self;
    if (headRefresh) {
        
        self.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
           
            [sself refreshBegin:sself.header];
        }];
        
        self.tableView.mj_header = self.header;
        
    }else{
        self.tableView.mj_header = nil;
        self.header = nil;
    }
}

- (void)setFootRefresh:(BOOL)footRefresh{
    
    typeof(self)sself = self;
    if (footRefresh) {
        self.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            
            [sself refreshBegin:sself.footer];
            
        }];
        
        self.tableView.mj_footer = self.footer;
        
    }else{
        self.tableView.mj_footer = nil;
        self.footer = nil;
    }
}

- (void)refreshBegin:(MJRefreshComponent*)component{
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

#pragma mark - 导航相关
- (void)navigationLeftItemTitle:(NSString*)title image:(NSString*)imageName
                          target:(id)target action:(SEL)action{
    
    [self navigationLeftItemTitle:title image:imageName target:target action:action frame:CGRectMake(0, 0, 30, 30)];
}

- (void)navigationRightItemTitle:(NSString*)title image:(NSString*)imageName
                       target:(id)target action:(SEL)action{
    
    [self navigationRightItemTitle:title image:imageName target:target action:action frame:CGRectMake(0, 0, 30, 30)];
}

- (void)navigationRightItemTitle:(NSString*)title image:(NSString*)imageName
                          target:(id)target action:(SEL)action frame:(CGRect)frame{
    
    [self navigationItemTitle:title image:imageName target:target action:action frame:frame left:NO];
}

- (void)navigationLeftItemTitle:(NSString*)title image:(NSString*)imageName
                         target:(id)target action:(SEL)action frame:(CGRect)frame{
    
    [self navigationItemTitle:title image:imageName target:target action:action frame:frame left:YES];
}

- (void)navigationItemTitle:(NSString*)title image:(NSString*)imageName
                     target:(id)target action:(SEL)action frame:(CGRect)frame left:(BOOL)left{
    
    UIButton* button = [[UIButton alloc] initWithFrame:frame];
    
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    
    if (imageName) {
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    
    if (target && action) {
        
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    if (left) {
        [self navigationLeftItem:item rightItem:nil];
    }else{
        [self navigationLeftItem:nil rightItem:item];
    }
}

- (void)navigationRightItem:(UIBarButtonItem*)rightItem{
    [self navigationLeftItem:nil rightItem:rightItem];
}

- (void)navigationLeftItem:(UIBarButtonItem*)leftItem{
    [self navigationLeftItem:leftItem rightItem:nil];
}

- (void)navigationLeftItem:(UIBarButtonItem*)leftItem rightItem:(UIBarButtonItem*)rightItem{
    NSArray* leftItemArray = nil;
    NSArray* rightItemArray = nil;
    if (leftItem) {
        leftItemArray = @[leftItem];
    }
    
    if (rightItem) {
        rightItemArray = @[rightItem];
    }
    
    [self navigationLeftItems:leftItemArray rightItems:rightItemArray];
}

- (void)navigationRightItems:(NSArray<UIBarButtonItem*>*)rightItems{
    [self navigationLeftItems:nil rightItems:rightItems];
}

- (void)navigationLeftItems:(NSArray<UIBarButtonItem*>*)leftItems{
    
    [self navigationLeftItems:leftItems rightItems:nil];
}

- (void)navigationLeftItems:(NSArray<UIBarButtonItem*>*)leftItems rightItems:(NSArray<UIBarButtonItem*>*)rightItems{
    
    if (leftItems) {
        self.navigationItem.leftBarButtonItems = leftItems;
    }
    
    if (rightItems) {
        self.navigationItem.rightBarButtonItems = rightItems;
    }
}


#pragma mark - 键盘相关
- (void)registerKeyboradShowAndHide{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboradShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboradHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboradShow:(NSNotification*)ifity{
    
    CGRect frame = [ifity.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    
    CGSize size = frame.size;
    
    CGFloat time = [ifity.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    
    if (self.autoTableViewHeight) {
        if (self.tableView.superview != nil) {
            //减少tableView的高度
            [self.tableView changeHeight:self.view.height - self.tableView.y - size.height];
        }
    }
    
    if ([self respondsToSelector:@selector(keyboradShowComplateAtHeight:andAnimationTime:)]) {
        [self keyboradShowComplateAtHeight:size.height andAnimationTime:time];
    }
    
}

- (void)keyboradHide:(NSNotification*)ifity{
    
    CGFloat time = [ifity.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    
    if (self.autoTableViewHeight) {
        if (self.tableView.superview != nil) {
            //回复tableView的高度
            [self.tableView changeHeight:self.view.height - self.tableView.y];
        }
    }
    
    if ([self respondsToSelector:@selector(keyboradHideComplateAtAnimationTime:)]) {
        [self keyboradHideComplateAtAnimationTime:time];
    }
}

#pragma mark - 适配X
- (CGFloat)toolHeight{
    
    if (_toolHeight == 0) {
        XFSystemDeviceType type = [XFSystem getDeviceType];
        
        if (type != XFSystemDeviceTypeX) {
            _toolHeight = 49;
        }else{
            _toolHeight = 83;
        }
    }
    
    return _toolHeight;
}

- (CGFloat)naviHeight{
    
    if (_naviHeight == 0) {
        XFSystemDeviceType type = [XFSystem getDeviceType];
        
        if (type != XFSystemDeviceTypeX) {
            _naviHeight = 64;
        }else{
            _naviHeight = 88;
        }
    }
    
    return _naviHeight;
}

- (CGFloat)naviSafeGap{
    
    if (_naviSafeGap == 0) {
        XFSystemDeviceType type = [XFSystem getDeviceType];
        
        if (type != XFSystemDeviceTypeX) {
            _naviSafeGap = 0;
        }else{
            _naviSafeGap = NAVI_SAFE_GAP;
        }
    }
    return _naviSafeGap;
}

- (CGFloat)tabSafeGap{
    
    if (_tabSafeGap == 0) {
        XFSystemDeviceType type = [XFSystem getDeviceType];
        
        if (type != XFSystemDeviceTypeX) {
            _tabSafeGap = 0;
        }else{
            _tabSafeGap = TOOLBAR_SAFE_GAP;
        }
    }
    return _tabSafeGap;
}

- (UIView *)contentView{
    
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
    }
    return _contentView;
}

- (void)resumeTableView{
    
    typeof(self)sself = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.bottom.left.right.equalTo(sself.contentView);
        
    }];
    
}

- (void)goBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}




@end
